import { defineStore } from "pinia";
import { ref } from 'vue'
import Cookies from 'js-cookie';
import { useRouter } from "vue-router";


export const useUserStore = defineStore("user", () => {
    const router = useRouter()

    // -1.菜单栏折叠状态
    const isCollapse = ref(true) // true 折叠 false 展开
    const updateIsCollapse = (newIsCollapse) => { // 更新是否折叠
        isCollapse.value = newIsCollapse
    }

    // 0.是否登录
    const isLogin = ref(false)
    const updateIsLogin = (newIsLogin) => { // 更新是否登录
        isLogin.value = newIsLogin
    }

    // 0.1 主题标题
    const themeTitle = ref('默认标题')
    const updateThemeTitle = (newThemeTitle) => { // 更新主题标题
        themeTitle.value = newThemeTitle
    }

    // 0.2 登陆成功后，进行检测
    // 只有登录完才检测，第二次进入首页不检测
    const isDetection = ref(1) // 1 未检测  2 已检测
    const updateIsDetection = (newIsDetection) => { // 更新是否检测
        isDetection.value = newIsDetection
    }

    // 1.token
    const token = ref('')
    // 更新token
    const updateToken = (newToken) => {
        token.value = newToken;
    }

    // 2.是否选择客户
    const isSelectCustomer = ref(1) // 1 未选择/新增 2 已选择
    const updateIsSelectCustomer = (newIsSelectCustomer) => {
        isSelectCustomer.value = newIsSelectCustomer;
    }

    // 3.被选择的客户信息
    const customerInfo = ref({
        id: null,
        customerId: '',
        customerName: "",
        customerTel: "",
        sex: null,
        birthday: null,
        address: '',
        occupationName: "",
        eyeUsageDistanceName: "",
        workEnvName: "",
        headUrl: ''
    })
    const updateCustomerInfo = (newUserInfo) => {
        customerInfo.value = newUserInfo;
    }

    // 4.人脸测量
    // 4.1 人脸正面检测状态
    const measureStatus = ref(1) // 1.开始测量 2.测量完成 3.结束测量
    const updateMeasureStatus = (newMeasureStatus) => {
        measureStatus.value = newMeasureStatus;
    }

    // 4.2 配镜测量状态
    const glassesMeasureStatus = ref(1) // 1.开始测量 2.测量完成 3.结束测量
    const updateGlassesMeasureStatus = (newGlassesMeasureStatus) => {
        glassesMeasureStatus.value = newGlassesMeasureStatus;
    }

    // 5.3D试戴测量状态
    const tryOnStatus = ref(1) // 1.开始检查  2.重新检查
    const updateTryOnStatus = (newTryOnStatus) => {
        tryOnStatus.value = newTryOnStatus;
    }

    // ---------------  管理模式-----------------
    // 1.首页
    const cutStatus = ref(1) // 1.默认首页 2.检查流程数据页面
    const updateCutStatus = (newCutStatus) => {
        cutStatus.value = newCutStatus;
    }

    // 2.流程指引
    const flowActiveIndex = ref(0)
    const updateFlowActiveIndex = (newFlowActiveIndex) => {
        flowActiveIndex.value = newFlowActiveIndex;
    }

    // 3.流程指引 -- 检测数据表单
    const checkDetailForm = ref({
        id: null,// 检查单id
        relationId: null, // 受检人id
        relationName: '', // 受检人姓名
        phone: '', // 受检人手机号
        sex: null, // 性别 1.男 2.女 3.未知
        age: null, // 年龄
        address: '', // 地址
        customerName: '',
        customerTel: '',
        baseRelationName: '',
        projectList: []
    })
    const updateCheckDetailForm = (newCheckDetailForm) => { // 更新检测表单
        checkDetailForm.value = newCheckDetailForm
    }

    // 控制显示隐藏对应区域
    const updateCheckItem = (newCheckItem) => {
        checkDetailForm.value.projectList.forEach(item => {
            newCheckItem.includes(item.regionId) ? item.isChecked = true : item.isChecked = false
        })
    }

    // 4.区域数据
    const regionData = ref({})
    const updateRegionData = (newRegionData) => { // 更新区域数据
        regionData.value = newRegionData
    }

    // 5.方案里面的tag标签值
    const tabName = ref('1')
    const updateTagName = (newTagName) => { // 更新tag标签值
        tabName.value = newTagName
    }

    // 6.配镜方案的编辑状态
    const opticianEditStatus = ref(1) // 1.阅读模式 2.编辑模式
    const updateOpticianEditStatus = (newEditStatus) => { // 更新tag标签值
        opticianEditStatus.value = newEditStatus
    }

    // 7.数据分析--设备分析
    const device_info = ref('')
    const updateDeviceInfo = (newDeviceInfo) => { // 获取设备信息
        device_info.value = newDeviceInfo
    }


    // 重置store状态
    const resetStore = () => {
        flowActiveIndex.value = 0
        isCollapse.value = false
        isLogin.value = false
        token.value = ''
        themeTitle.value = '默认标题'
        isDetection.value = 1
        isSelectCustomer.value = 1
        customerInfo.value = {
            id: null,
            customerId: '',
            customerName: "",
            customerTel: "",
            sex: null,
            birthday: null,
            address: '',
            occupationName: "",
            eyeUsageDistanceName: "",
            workEnvName: "",
            headUrl: ''
        }
        measureStatus.value = 1
        glassesMeasureStatus.value = 1

        cutStatus.value = 1
        checkDetailForm.value = {}
        regionData.value = {}
        tabName.value = '1'
        opticianEditStatus.value = 1
        device_info.value = ''
    }
    // 退出登录
    const logout = () => {
        resetStore()

        setTimeout(() => {
            router.push({ name: 'login' })
        }, 100)
    }

    return {
        isCollapse,
        updateIsCollapse,
        isLogin,
        updateIsLogin,
        themeTitle,
        updateThemeTitle,
        isDetection,
        updateIsDetection,
        token,
        updateToken,
        customerInfo,
        updateCustomerInfo,
        isSelectCustomer,
        updateIsSelectCustomer,
        glassesMeasureStatus,
        updateGlassesMeasureStatus,
        measureStatus,
        updateMeasureStatus,
        tryOnStatus,
        updateTryOnStatus,

        cutStatus,
        updateCutStatus,
        flowActiveIndex,
        updateFlowActiveIndex,
        checkDetailForm,
        updateCheckDetailForm,
        updateCheckItem,
        regionData,
        updateRegionData,
        tabName,
        updateTagName,
        opticianEditStatus,
        updateOpticianEditStatus,
        device_info,
        updateDeviceInfo,
        logout
    }
})